package test;

import java.io.IOException;
import java.sql.Date;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import uci.rt.ccr1c.engine.reader.ConfigReader;
import uci.rt.ccr1c.engine.reader.GridPointFileReader;
import uci.rt.ccr1c.scentrans.domain.GridPoint;
import uci.rt.pricing.date.calendar.CalendarException;
import uci.utils.config.ReplaceUtils;
import uci.utils.files.FileParserException;

public class BrownianBrifge
{

	/**
	 * @param args
	 * @throws FileParserException 
	 * @throws CalendarException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws CalendarException, FileParserException, IOException
	{
		Date aod= new Date(2013-1900, 04, 13);
		List<GridPoint> gridPoints;
		String fnSchedule = "C:/data/R1/matrix/ccr-norisk/2013/20130513/20130513_schedule365.txt";
		System.out.println("Read gridpoints from " + fnSchedule);
		GridPointFileReader gpReader = new GridPointFileReader(aod, fnSchedule);
		gpReader.process();
		gridPoints = gpReader.getGridPoints();
		
		Collections.sort(gridPoints,  new Comparator<GridPoint>(){
			  public int compare(GridPoint s1, GridPoint s2) {

				  try
				{
					if (s1.getExDate() < s2.getExDate())
					        return -1;
					    else if (s1.getExDate() == s2.getExDate())
					        return 0;
					    else
					        return 1;
				}
				catch (CalendarException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return 1;
				  }
				});
		

		for (GridPoint gp : gridPoints)
		{
			if(gp.getName().equalsIgnoreCase("fvcirscen1d_20d.bin")){
				System.out.println();
			}
			System.out.println(gp.getName()+"\t"+ gp.getExDate() + "\t" + gp.getYearFraction250()+"\t"+gp.getDate());
			System.out.println(gp.getDate());
		}
		
		
		String configFile ="C:/data/R1/matrix/ccr-norisk/2013/20130513/20130513_config.txt";
		ConfigReader cfgReader = new ConfigReader(configFile);
		cfgReader.process();	
		long predefinedSeed = cfgReader.getSeed();
		
		// bridger needs year fraction, fixings contain exDates from gridPoints. Set up a
		// Map ...
		Map<Double,Double> mapExDate2YearFraction = new HashMap<Double, Double>();
		Map<Double,Double> mapYearFraction2ExDate = new HashMap<Double, Double>();
		for (GridPoint gp : gridPoints)
		{
			mapExDate2YearFraction.put(gp.getExDate(), gp.getYearFraction250());
			mapYearFraction2ExDate.put(gp.getYearFraction250(),gp.getExDate());
		}
		
		// fill gpYearFraction from gridpoints
		// fixings of scen#0, 1st column is exDate (already sorted asc by date)
//		double[][] tmp = fixings4Scenarios.get(0);
//		gpYearFractions = new double[tmp.length];
//		log.info("### calculate gpYearFractions");
//		for (int i = 0; i < tmp.length; i++)
//		{
//			double exDate = tmp[i][0];
//			Double t = mapExDate2YearFraction.get(exDate);
//			log.info("### exDate="+exDate+" t="+t);
//			gpYearFractions[i] = t;
//		}

	}

}
